package com.dandelion.manager.system.web.controller;

import java.util.List;

import org.hyperic.sigar.Sigar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dandelion.commons.annotation.SystemLog;
import com.dandelion.commons.constants.BaseConst;
import com.dandelion.commons.utils.PropertyUtils;
import com.dandelion.commons.utils.SystemInfo;
import com.dandelion.commons.web.controller.BaseController;
import com.dandelion.commons.web.model.AjaxResponse;
import com.dandelion.manager.system.service.MonitorService;
import com.dandelion.manager.system.web.form.MoitorThreadinfoResult;
import com.dandelion.manager.system.web.form.MonitorCacheinfo;
import com.dandelion.manager.system.web.form.ServerInfoFormMap;

@Controller
@RequestMapping("/manager/system/monitor")
public class MonitorController extends BaseController {
	
	@Autowired
	private MonitorService monitorService ;
	
	/**
	 * 阿里巴巴 Druid 监控
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/druid", method=RequestMethod.GET)
	public String druid() {
		return "redirect:/druid";
	}
	
	/**
	 * 实时监控页面
	 * @param model
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[系统监控]", method="系统实时监控页面")
	@RequestMapping(value="/monitor_main", method=RequestMethod.GET)
	public String monitor_main(Model model) {
		model.addAttribute("cpu", PropertyUtils.getValue("system.monitor.cpu")); 
		model.addAttribute("jvm", PropertyUtils.getValue("system.monitor.jvm"));
		model.addAttribute("ram", PropertyUtils.getValue("system.monitor.ram"));
		return viewName("monitor_main");
	}
	
	/**
	 * 缓存管理页面
	 * @param model
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[系统监控]", method="系统缓存管理页面")
	@RequestMapping(value="/monitor_cache_main", method=RequestMethod.GET)
	public String monitor_cache_main(Model model) {
		return viewName("monitor_cache_main");
	}
	
	/**
	 * 线程信息页面
	 * @param model
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[系统监控]", method="系统线程信息页面")
	@RequestMapping(value="/monitor_thread_main", method=RequestMethod.GET)
	public String monitor_thread_main(Model model) {
		return viewName("monitor_thread_main");
	}
	
	/**
	 * 获取系统信息
	 * @return
	 */
	@RequestMapping(value="/monitor_system_info", method=RequestMethod.GET)
	public @ResponseBody ServerInfoFormMap monitor_system_info(Model model) {
		return SystemInfo.SystemProperty() ;
	}
	
	/**
	 * 实时监控信息
	 * @return
	 */
	@RequestMapping(value="/monitor_usage", method=RequestMethod.GET)
	@ResponseBody
	public ServerInfoFormMap usage() {
		return SystemInfo.usage(new Sigar()) ;
	}
	
	/**
	 * 缓存管理
	 * @return
	 */
	@RequestMapping(value="/monitor_cache", method=RequestMethod.GET)
	public @ResponseBody List<MonitorCacheinfo> monitor_cache() {
		return this.monitorService.getCacheInfo() ;
	}
	
	/**
	 * 刷新缓存
	 * @param cacheNames
	 * @return
	 */
	@SystemLog(module="系统管理", method="刷新缓存")
	@RequestMapping(value="/monitor_freshenCache", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse monitor_freshenCache(String cacheNames) {
		return this.monitorService.freshenCache(cacheNames) ;
	}
	
	/**
	 * 线程信息
	 * @return
	 */
	@RequestMapping(value="/monitor_thread", method=RequestMethod.GET)
	public @ResponseBody MoitorThreadinfoResult monitor_thread() {
		return this.monitorService.getThreadInfo() ;
	}
	
	

}
